import { Component, Input, OnInit } from '@angular/core';
import { SFSchema } from '@delon/form';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';

@Component({
  selector: 'dic-edit',
  templateUrl: './dic-edit.component.html',
  styles: []
})
export class DicEditComponent implements OnInit {
  @Input()type: 0 | 1;
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
    // this.getDic(this.param.id);
  };
  param: any;
  showForm = false;
  schema: SFSchema = {
    properties: {
      deviceTypeCode: {
        type: 'string',
        title: 'deviceTypeCode',

      },
      deviceTypeName: {
        type: 'string',
        title: 'deviceTypeName'
      },
      eventCode: {
        type: 'string',
        title: 'eventCode'
      },
      eventName :{
        type: 'string',
        title: 'eventName'
      },
      eventType :{
        type: 'string',
        title: 'eventType'
      },
      eventTypeImg: {
        type: 'string',
        title: 'eventTypeImg'
      },
      eventClass: {
        type: 'string',
        title: 'eventClass'
      },
      eventSign: {
        type: 'string',
        title: 'eventSign'
      },
      eventDescribe: {
        type: 'string',
        title: 'eventDescribe'
      },
      eventResponseTime: {
        type: 'string',
        title: 'eventResponseTime'
      },
      eventDisposeTime: {
        type: 'string',
        title: 'eventDisposeTime'
      }
      
    },
    required: []
  }
  
  getDic(id: string): Promise<any>{
    return this.http.request<any>('GET', '/core/event/dictionary/'+id).toPromise();
  }
  formSubmit(data) {
    const body: any = {
      deviceTypeCode: data.deviceTypeCode,
      deviceTypeName: data.deviceTypeName,
      eventCode: data.eventCode,
      eventName: data.eventName,
      eventType: data.eventType,
      eventTypeImg: data.eventTypeImg,

      eventClass: data.eventClass,
      eventSign: data.eventSign,
      eventDescribe: data.eventDescribe,
      eventResponseTime: data.eventResponseTime,
      eventDisposeTime: data.eventDisposeTime,
    };
    const url = this.type === 0 ? '/core/event/dictionary' : '/core/event/dictionary/'+data.id;
    const method = this.type === 0 ? 'POST' : 'PUT'
    this.http.request<any>(method, url,  { body}).subscribe(res => {
      console.log(res)
      if (res.status === '200' || res.status === '201') {
        this.msg.success('操作成功');
        this.modalRef.destroy('ok');
      } else {
        this.msg.error(res.message)
      }
    })

  }
  constructor(
    private api: ApiService,
    private http: HttpClient,
    private msg: NzMessageService,
    private modalRef: NzModalRef
  ) { }

  async ngOnInit() {
    this.showForm = true;
  }

}
